package com.zeedev.flyingsaucer.beers.activities.home;

import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableString;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.google.inject.Inject;
import com.zeedev.flyingsaucer.beers.activities.home.interfaces.HomeController;
import com.zeedev.flyingsaucer.global.UfoClubApp;
import com.zeedev.flyingsaucer.ufoclub.R;
import mobi.zeedev.flyingsaucer.common.CredentialsModule.impl.HomeInfo;
import mobi.zeedev.flyingsaucer.common.WebLinkModule.interfaces.UfoClubClient;
import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;

public class HomeActivity extends RoboActivity {

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Constants
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

//    public static final String TAG = "HomeActivity";

//    public static final int INIT_LOGIN = 0;
//    public static final int VIEW_BEERS = 1;
//    public static final int VIEW_NEW_BEERS = 2;
//    public static final int VIEW_ABOUT_DIALOG = 3;

//    private static final String HOME_STATE = "HOME_STATE";
//    private static final String OLD_HOME_INFO = "OLD_HOME_INFO";

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // HomeActivty State
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Inject
    UfoClubApp app;

    @Inject
    HomeController controller;

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // GUI Referencnes
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @InjectView(R.id.buttonViewBeerList)
    Button buttonViewBeers;

    @InjectView(R.id.buttonViewNewBeers)
    Button buttonViewNewBeers;

    @InjectView(R.id.textWelcomeBack)
    TextView textWelcomeBack;

    @InjectView(R.id.textNumberBeers)
    TextView textBeerCount;

    @InjectView(R.id.textUfoClubHeading)
    TextView ufoClubHeading;

    @InjectView(R.id.buttonViewOtherStores)
    Button buttonViewAllStoresList;

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Activity Life-cycle
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        this.setContentView(R.layout.home2);

        buttonViewBeers.setOnClickListener(viewBeerListOnClickListener);
        buttonViewNewBeers.setOnClickListener(viewNewBeerListOnClickListener);
        buttonViewAllStoresList.setOnClickListener(viewOtherStoresOnClickListener);

        Typeface tf = Typeface.createFromAsset(getAssets(), "ReenieBeanie.ttf");
        ufoClubHeading.setTypeface(tf, Typeface.ITALIC);
        SpannableString content = new SpannableString("Flying Saucer\nU.F.O. Club");
        ufoClubHeading.setText(content);

        controller.init();

    }

/*    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//		Log.d(TAG,"onActivityResult.");

        switch (requestCode) {
//            case HomeActivity.INIT_LOGIN:
//                if (resultCode == Activity.RESULT_CANCELED) {
//
////                    this.homeState = HomeState.VIEWING_LOGIN;
////                    this.goHome();
//
//                    controller.minimizeUfoClubApp();
//
//                } else {
////                    this.enterState(HomeState.SHOULD_REFRESH);
////                    controller.refresh(true);
//                }


//                break;
            default:
//                this.enterState(HomeState.SHOULD_REFRESH);
//                controller.refresh(false);
                break;

        }

    }*/

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_home_logout: {

//                this.enterState(HomeState.LOGOUT);
                controller.logout();

                return true;
            }
            case R.id.menu_home_refresh: {

//                this.startLoginQuery();
//                this.enterState(HomeState.REFRESHING_ACTIVE);
//                controller.refresh(true);
                //TODO FIX?

                return true;
            }

            case R.id.menu_home_about: {
//                this.enterState(HomeState.VIEW_ABOUT_DIALOG);
                controller.viewAbout();

                return true;
            }
        }
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateViews();
    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // State Saving
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Inject
    UfoClubClient client;

    public void updateViews() {

//        final HomeInfo homeinfo = controller.getModel().getHomeInfo();

        final HomeInfo homeInfo = client.getHomeInfoCache();

        if (homeInfo.isEmpty()) {
            textWelcomeBack.setVisibility(View.INVISIBLE);
            textBeerCount.setVisibility(View.INVISIBLE);
            this.buttonViewBeers.setVisibility(View.INVISIBLE);
            this.buttonViewNewBeers.setVisibility(View.INVISIBLE);
        } else {
            textWelcomeBack.setVisibility(View.VISIBLE);
            textBeerCount.setVisibility(View.VISIBLE);

            this.buttonViewBeers.setVisibility(View.VISIBLE);
            this.buttonViewNewBeers.setVisibility(View.VISIBLE);

            textWelcomeBack.setText("Welcome back, " + homeInfo.get(HomeInfo.WELCOME_NAME) + "!");
            String beerNumberText = String.format("You've sampled %s of %s beers.", homeInfo.get(HomeInfo.PLATE_CURRENT_TOTAL), homeInfo.get(HomeInfo.PLATE_REQUIRED_TOTAL));
            textBeerCount.setText(beerNumberText);
        }
    }

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    // Click Listeners
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


/*
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //return super.onKeyDown(keyCode, event);
        if (keyCode == KeyEvent.KEYCODE_BACK) {

//            controller.minimizeUfoClubApp();

//            this

            return true;
        } else {
            return false;
        }

    }
*/

    private OnClickListener viewNewBeerListOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            controller.viewNewBeers();
        }
    };

    private OnClickListener viewBeerListOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            controller.viewBeers();
        }
    };

/*    private OnCancelListener progressCancelListener = new OnCancelListener() {
        @Override
        public void onCancel(DialogInterface arg0) {
            //implement this
        }
    };*/

    private OnClickListener viewOtherStoresOnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            controller.viewStores();
        }

    };


}
